#include <bits/stdc++.h>
using namespace std;
const int maxn = 9;
int s[maxn];
int N, maxd;


int check(const int a[]) {
    int wrong = 0;
    for (int i = 0; i < N - 1; ++i) {
        if (a[i] + 1 != a[i + 1]) ++wrong;
    }
    if (a[N - 1] != N) ++wrong;
    return wrong;
}

bool IDA(int d, int a[]) {
    int W = check(a);
    if (d == maxd) return !W;
    if (3 * maxd - 3 * d < W) return false;

    for (int l = 0; l < N; ++l) {
        for (int r = l; r < N; ++r) {
            int remain[maxn];
            int cnt = 0;
            for (int i = 0; i < N; ++i) {
                if (i < l || i > r) remain[cnt++] = a[i];
            }

            for (int in = 0; in < cnt + 1; ++in) {
                int nex[maxn], cnt2 = 0;
                for (int p = 0; p < in; ++p) nex[cnt2++] = remain[p];
                for (int p = l; p <= r; ++p) nex[cnt2++] = a[p];
                for (int p = in; p < cnt; ++p) nex[cnt2++] = remain[p];

                if (IDA(d + 1, nex)) return true;
            }

        }
    }
    return false;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 0;
    while (cin >> N && N) {
        cout << "Case " << ++T << ": ";
        for (int i = 0; i < N; ++i) {
            cin >> s[i];
        }
        if(!check(s)) {cout << "0\n"; continue;}
        for (maxd = 1; maxd < 9; ++maxd) {
            if (IDA(0, s)) {
                cout << maxd << "\n";
                break;
            }
        }
    }
    return 0;
}